/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*                                                                  *
*        *************************************************         *
*        *©2021-2031 HT  Corporation All rights reserved *         *
*        *************************************************         *
*                                                                  *
* FileName    : demo_rtsp.c                                        *
*                                                                  *
* Author      : linus                                              *
*                                                                  *
* Email       : luoyaojun@haitutech.com                            *
*                                                                  *
* Date        : 2023-1-28                                          *
*                                                                  *
* Description :                                                    *
*                                                                  *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "demo_rtsp.h"
struct rtsp_chn_config{

	HANDLE handle;
	
	char chn_name[8];

};

struct rtsp_config{
	MFormat fmt;

	HT_U32 port;
	
	struct rtsp_chn_config chn[RTSP_MAX_CHN_NUM];

	void *priv;
};
static void EventCallback(const RtspCasterEvent *event, void *context)
{
}

static struct rtsp_config *rtsp_service_init(HT_U32 port, HT_U32 width, HT_U32 height, HT_U32 framerate, HT_S32 codec)
{
	struct rtsp_config *pconfig = NULL;
	HT_S32 ret = -1;
	
	pconfig = (struct rtsp_config *)calloc(1, sizeof(struct rtsp_config));

	if (!pconfig)
		return NULL;
	
	ret = caster_init(port, NULL);

	if (ret)
	{
		free(pconfig);
		return NULL;
	}
	
	caster_set_event_callback(EventCallback, NULL);
	pconfig->fmt.width 		= width;
	pconfig->fmt.height 	= height;
	pconfig->fmt.codec 		= MCODEC_H264;
	pconfig->fmt.clockRate 	= 1000000;
	pconfig->fmt.framerate 	= framerate;

	return pconfig;
}

struct rtsp_config *rtsp_service_init_h264(HT_U32 port, HT_U32 width, HT_U32 height, HT_U32 framerate)
{
	return (rtsp_service_init(port, width, height, framerate, MCODEC_H264));
}

struct rtsp_config * rtsp_service_init_h265(HT_U32 port, HT_U32 width, HT_U32 height, HT_U32 framerate)
{
	return (rtsp_service_init(port, width, height, framerate, MCODEC_H265));
}

/*
不同chn可以通过写入不同分辨率的venc数据，和rtsp_service_init中的宽高无关
*/
HT_S32 rtsp_service_request_chn(struct rtsp_config *pconfig, HT_U32 chn)
{

	if (!pconfig)
		return -1;
	
	if (chn <0 || chn >=RTSP_MAX_CHN_NUM)
		return -2;

	snprintf(pconfig->chn[chn].chn_name, sizeof(pconfig->chn[chn].chn_name), "chn%d", chn);
	
	return (caster_chl_open(&pconfig->chn[chn].handle, pconfig->chn[chn].chn_name, &pconfig->fmt));
}

HT_VOID rtsp_service_set_priv(struct rtsp_config *pconfig, HT_VOID *v)
{
	if (pconfig)
		pconfig->priv = v;
}

HT_S32 rtsp_service_write_ch(struct rtsp_config *pconfig, HT_U32 chn, HT_U8* data, HT_S32 len, HT_U64 pts)
{
	MPacket mpkt;

	if (!pconfig)
		return -1;
	
	memset(&mpkt, 0, sizeof(MPacket));
	
	mpkt.data = (uint8_t *)data;
	mpkt.size = len;
	mpkt.pts = pts;

	caster_chl_write_video(pconfig->chn[chn].handle, &mpkt);
}


